/*
 * Created by JFormDesigner on Mon Jun 26 15:15:01 CST 2023
 */

package org.scct.chenhui.frames;

import org.scct.chenhui.dao.BookDao;
import org.scct.chenhui.dao.BookPublishDao;
import org.scct.chenhui.dao.BookTypeDao;
import org.scct.chenhui.model.BookBean;
import org.scct.chenhui.model.BookPublishBean;
import org.scct.chenhui.model.BookTypeBean;
import org.scct.chenhui.utils.ActionDo;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.*;

/**
 * @author Administrator
 */
public class DlgBookAdd extends JDialog {


    private ActionDo actionDo;

    public DlgBookAdd(Window owner, ActionDo actionDo) {
        super(owner);
        initComponents();
        this.actionDo=actionDo;
        initData();
    }

    private void initData() {
//     初始化工作
        //1.数量
        SpinnerNumberModel spinnerNumberModel = new SpinnerNumberModel(1, 1, 99, 1);
        txtNumber.setModel(spinnerNumberModel);
//        2.类别下拉列表
        BookTypeDao bookTypeDao = new BookTypeDao(); //创建图书列表数据库访问对象
        List<BookTypeBean> types = bookTypeDao.getAllTypes(); //获取所以所有图书类别
        Vector<BookTypeBean> vtypes = new Vector<>(types);  //将arrayList转换为vector列表，用于创建列表模型
        DefaultComboBoxModel typeModel = new DefaultComboBoxModel(vtypes); //创建列表模型
        cboType.setModel(typeModel); //为下拉列表设置数据模型，显示列表数据
//        3.出版社下拉列表
        BookPublishDao bookPublishDao = new BookPublishDao();
        List<BookPublishBean> publishers = bookPublishDao.getAllPubliser();
        Vector<BookPublishBean> vpublishers = new Vector<>(publishers);
        DefaultComboBoxModel pubModel = new DefaultComboBoxModel(vpublishers);
        cboPub.setModel(pubModel);
//        4.状态下拉列表
        cboStatus.addItem("下架"); //0
        cboStatus.addItem("上架");  //1
        //设置选中下拉列表的第1项，从0开始计数，也就是显示上架项
        cboStatus.setSelectedIndex(1);

        btnAdd.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                INSERT bm_book VALUES(null,"水浒传",1,"施耐庵",2,59.99,50,1,"北宋末年的农民起义的故事")
                //1.获取内容
                String name = txtName.getText();

                BookTypeBean bookTypeBean = (BookTypeBean) cboType.getSelectedItem();
                int typeid=bookTypeBean.getId();

                String author = txtAuthor.getText();
                BookPublishBean publisher = (BookPublishBean) cboPub.getSelectedItem();

                int pubid = publisher.getId();
                String price = txtPrice.getText();
                int num = Integer.parseInt(txtNumber.getValue().toString());
                int status = cboStatus.getSelectedIndex();
                String remark = txtRemark.getText();

                //2验证
                if(name.trim().length()==0)
                {
                    JOptionPane.showMessageDialog(null,"请输入书名");
                    return;
                }
                if(author.trim().length()==0)
                {
                    JOptionPane.showMessageDialog(null,"请输入作者");
                    return;
                }
                if(remark.trim().length()==0)
                {
                    JOptionPane.showMessageDialog(null,"请输入备注");
                    return;
                }
                // 0.2  12.5  50  8
                if(!price.matches("^\\d[0-9.]+$"))
                {
                    JOptionPane.showMessageDialog(null,"请输入正确的价格");
                    return;
                }
                //3添加
                BookBean book = new BookBean();
                book.setBookName(name);
                book.setAuthor(author);
                book.setNumber(num);
                book.setPrice(Float.parseFloat(price));
                book.setPublishID(pubid);
                book.setRemark(remark);
                book.setStatus(status);
                book.setTypeId(typeid);
                BookDao bookDao = new BookDao();
                if(bookDao.addbook(book))
                {
                    JOptionPane.showMessageDialog(null,"添加成功");
                    actionDo.afterdo(null);
                    dispose();
                }else{
                    JOptionPane.showMessageDialog(null,"添加失败");
                }


            }
        });

        btnCancel.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - brightch
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        label9 = new JLabel();
        btnAdd = new JButton();
        btnCancel = new JButton();
        txtName = new JTextField();
        txtAuthor = new JTextField();
        txtPrice = new JTextField();
        txtNumber = new JSpinner();
        cboType = new JComboBox();
        cboPub = new JComboBox();
        cboStatus = new JComboBox();
        scrollPane1 = new JScrollPane();
        txtRemark = new JTextArea();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u56fe\u4e66\u6dfb\u52a0");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(240, 20), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u4e66\u540d");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(40, 65), label2.getPreferredSize()));

        //---- label3 ----
        label3.setText("\u4f5c\u8005");
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(40, 110), label3.getPreferredSize()));

        //---- label4 ----
        label4.setText("\u4ef7\u683c");
        contentPane.add(label4);
        label4.setBounds(new Rectangle(new Point(40, 150), label4.getPreferredSize()));

        //---- label5 ----
        label5.setText("\u6570\u91cf");
        contentPane.add(label5);
        label5.setBounds(new Rectangle(new Point(40, 195), label5.getPreferredSize()));

        //---- label6 ----
        label6.setText("\u7c7b\u522b");
        contentPane.add(label6);
        label6.setBounds(new Rectangle(new Point(330, 70), label6.getPreferredSize()));

        //---- label7 ----
        label7.setText("\u51fa\u7248\u793e");
        contentPane.add(label7);
        label7.setBounds(new Rectangle(new Point(325, 110), label7.getPreferredSize()));

        //---- label8 ----
        label8.setText("\u72b6\u6001");
        contentPane.add(label8);
        label8.setBounds(new Rectangle(new Point(330, 155), label8.getPreferredSize()));

        //---- label9 ----
        label9.setText("\u5907\u6ce8");
        contentPane.add(label9);
        label9.setBounds(new Rectangle(new Point(330, 195), label9.getPreferredSize()));

        //---- btnAdd ----
        btnAdd.setText("\u6dfb\u52a0");
        contentPane.add(btnAdd);
        btnAdd.setBounds(new Rectangle(new Point(75, 265), btnAdd.getPreferredSize()));

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        contentPane.add(btnCancel);
        btnCancel.setBounds(new Rectangle(new Point(225, 265), btnCancel.getPreferredSize()));
        contentPane.add(txtName);
        txtName.setBounds(85, 60, 210, txtName.getPreferredSize().height);
        contentPane.add(txtAuthor);
        txtAuthor.setBounds(85, 105, 210, txtAuthor.getPreferredSize().height);
        contentPane.add(txtPrice);
        txtPrice.setBounds(85, 145, 210, txtPrice.getPreferredSize().height);
        contentPane.add(txtNumber);
        txtNumber.setBounds(85, 190, 185, txtNumber.getPreferredSize().height);
        contentPane.add(cboType);
        cboType.setBounds(365, 60, 145, cboType.getPreferredSize().height);
        contentPane.add(cboPub);
        cboPub.setBounds(365, 105, 145, cboPub.getPreferredSize().height);
        contentPane.add(cboStatus);
        cboStatus.setBounds(365, 145, 145, cboStatus.getPreferredSize().height);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(txtRemark);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(375, 195, 205, 75);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(605, 370);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - brightch
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    private JLabel label9;
    private JButton btnAdd;
    private JButton btnCancel;
    private JTextField txtName;
    private JTextField txtAuthor;
    private JTextField txtPrice;
    private JSpinner txtNumber;
    private JComboBox cboType;
    private JComboBox cboPub;
    private JComboBox cboStatus;
    private JScrollPane scrollPane1;
    private JTextArea txtRemark;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
